#ifndef _DISTANCE_MANAGER_HPP_
#define _DISTANCE_MANAGER_HPP_

#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <stdint.h>


struct DistancePair
{
    uint32_t id1;
    uint32_t id2;
    double similarity;
};

class DistancePairComparator
{
public:
    bool operator()(const DistancePair &pa, const DistancePair &pb)
    {
        if (pa.id1 < pb.id1)
            return true; 
        else if (pa.id1 == pb.id1 && pa.similarity > pb.similarity)
            return true;

        return false;
    }
};


class DistanceManager
{
    std::vector<DistancePair> _pairs;

public:
    DistanceManager()
    {
    }

    void ClearData()
    {
        _pairs.clear();
    }

    void AddDistancePair(uint32_t id1, uint32_t id2, double similarity)
    {
        DistancePair pair;
        pair.id1 = id1;
        pair.id2 = id2;
        pair.similarity = similarity;

        _pairs.push_back(pair);
    }


    template <typename BookName>
    void DumpResult(BookName &book_name)
    {
        std::sort(_pairs.begin(), _pairs.end(), DistancePairComparator());
        DistancePair base;
        base.id1 = 0xffffffff;
        int count = 0;

        for(size_t i = 0; i < _pairs.size(); i++) {
            const DistancePair &pair = _pairs[i];

            if (pair.id1 != base.id1) {
                printf("\n==%s : %d\n:", book_name.GetName(pair.id1).c_str(), pair.id1);
                base.id1 = pair.id1;
                count = 0;
            }

            count++;
            if (pair.similarity < 0.001 || count > 8) 
                continue;
            printf("%s:%d[%.3lf],     ", book_name.GetName(pair.id2).c_str(), pair.id2, pair.similarity);
        }
        printf("\n");
    }

};


#endif

